<?php


namespace App\Http\Helpers;

use App\Http\Services\MsgService;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class Http{

    private $client;

    function __construct($timeOut = 5)
    {
        $this->client = new Client(['timeout' => $timeOut]);

    }

    public function post($url, $data){
        try{
            $reponse = $this->client->request('POST', $url, [
                'body' => json_encode($data),
                'headers' => ['Content-Type' => 'application/json'],
//                'proxy' => 'http://web-proxy.oa.com:8080',
                'verify' => false
            ]);
        }catch(\Exception $e){
            Sign::writeLog(__CLASS__, $e->getMessage() . '___url_:'.$url.'___data_:' . json_encode($data), 'post');
            return MsgService::getInterfaceMsg(1000);
        }

        $body = $reponse->getBody();
        $content = json_decode($body, true);
        return $content;
    }

    public function get($url){
        try{
            $response = $this->client->request('GET', $url);
        }catch(\Exception $e){
            Log::error($e->getMessage());
            Sign::writeLog(__CLASS__, $e->getMessage(), 'get');
            return MsgService::getInterfaceMsg(1000);
        }

        $body = $response->getBody();
        $content = json_decode($body, true);
        return $content;
    }
}
